package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.TravelVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    @Override
    public PageInfo queryForList(TravelQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrder());
        return new PageInfo(travelMapper.queryForList(qo));
    }

    @Override
    public Travel get(Long id) {

        if (id != null) {
            return travelMapper.selectByPrimaryKey(id);
        }

        return null;
    }

    @Override
    public List<Travel> getTravelsByViewTop3(Long id) {

        if (id != null) {
            return travelMapper.getTravelsByViewTop3(id);
        }

        return null;
    }

    @Override
    public void changeReleaseState(Long id, Long state) {

        if (id != null && state != null) {

            travelMapper.changeReleaseState(id, state);

        }
    }

    @Override
    public TravelContent getContentById(Long id) {

        if (id != null) {

            return travelContentMapper.selectById(id);

        }
        return null;
    }

    @Override
    public Long saveOrUpdate(Travel travel) {

        if (travel != null) {

            //设置标题
            if (travel.getTravelContent()!= null){
                if(StringUtils.hasLength(travel.getTravelContent().getContent())){
                    if(travel.getTravelContent().getContent().length() > 200){

                        travel.setSummary(travel.getTravelContent().getContent().substring(0,200));
                    }else{

                        travel.setSummary(travel.getTravelContent().getContent());
                    }
                }
            }

            //保存记录
            if(travel.getId() == null){

                travel.setCreateTime(new Date());

                travelMapper.insert(travel);

            }else{

                travel.setLastUpdateTime(new Date());

                travelMapper.updateByPrimaryKey(travel);

            }

            //保存文章内容
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());

            travelContentMapper.insert(travelContent);

            return travel.getId();

        }

        return null;
    }

    @Override
    public List<Travel> list() {

        return travelMapper.selectAll();
    }

    @Override
    public void setVo(TravelVO vo) {

        if(vo != null){

            travelMapper.setVo(vo);

        }

    }

    @Override
    public List<Travel> queryTravlesByUid(Long id) {
        return travelMapper.selectTravlesByUid(id);
    }

    @Override
    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrder());
        return new PageInfo(travelMapper.selectForList(qo));
    }
}
